<?php

namespace app\admin\controller\family;

use app\common\controller\Backend;
use think\Db;

/**
 * 家庭问卷
 *
 * @icon fa fa-circle-o
 */
class Questionnaire extends Backend
{

    /**
     * Questionnaire模型对象
     * @var \app\admin\model\family\Questionnaire
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\family\Questionnaire;
    }

    public function index()
    {

        $group = $this->auth->getGroups();
        $user = $this->auth->getUserInfo();
        $groupId = $group[0]["group_id"];
        $this->assign('groupId', $groupId);
        $this->assignconfig('groupId', $groupId);
        if ($groupId == 1) {
            if ($this->request->isAjax()) {
                list($where, $sort, $order, $offset, $limit) = $this->buildparams();
                $list = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);
                $result = array("total" => $list->total(), "rows" => $list->items());
                return json($result);
            }
        } else{
            $list = $this->model->order('id', 'asc')->select();
            foreach ($list as $key => $value) {


                $answer = Db::table('jc_family_answer')->where('qid', '=', $value["id"])->where('adid', '=', $user["id"])->select();
                if (count($answer) > 0) {
                    $list[$key]["answer"] = $answer[0]["answer"];
                } else {
                    $list[$key]["answer"] = null;
                }
                $choice = json_decode($value["choice"], true);
                $list[$key]["choice"] =  $choice;
            }


            $score = Db::table('jc_family_answer')->where('adid', '=', $user["id"])->sum('score');
            $answerproposa = Db::table('jc_family_answerproposal')->select();
            $proposal="";
            foreach ($answerproposa as $key => $value) {
                if ($score > $value["min_score"] && $score <= $value["max_score"]) {
                    $proposal=$value["proposal"];
                }
                # code...
            }
            $this->assign('list', $list);
            $this->assign('score', $score);
            $this->assign('proposal', $proposal);
            if ($this->request->isPost()) {

                $params = $this->request->post("row/a");
                $answer2 = Db::table('jc_family_answer')->where('qid', '=', $value["id"])->where('adid', '=', $user["id"])->select();
                if (count($answer2) == 0) {

                    $scoresetting = Db::table('jc_family_scoresetting')->where('choice', '=', $params["answer"])->select();
                    foreach ($choice as $key => $value) {
                        if ($value["ingredients"] == $params["answer"]) {
                            $params["choice"] = $value["content"];
                        }
                    }
                    $params["adid"] = $user["id"];
                    $params["score"] = $scoresetting[0]["score"];
                    $add = Db::table('jc_family_answer')->insert($params);
                    if ($add) {
                        $this->success("提交成功");
                    } else {
                        $this->error("提交失败");
                    }
                } else {
                    $this->error("题目已提交");
                }
            }
        }
        return $this->view->fetch();
    }
    public function answer()
    {

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            echo $params;
            // $add = $this->model->insert($params);

        }
    }
}
